package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ww.yaodao.config.JWTConfig;
import com.ww.yaodao.mapper.UserMapper;
import com.ww.yaodao.model.bo.UserDO;
import com.ww.yaodao.model.vo.user.UserRespVO;
import com.ww.yaodao.service.UserService;
import com.ww.yaodao.model.vo.LoginVO;
import com.ww.yaodao.model.vo.user.UserVO;
import com.ww.yaodao.util.PasswordUtils;
import com.ww.yaodao.util.md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JWTConfig jwtConfig;

    @Override
    public Map<String, Object> login(LoginVO loginVo) throws Exception {
        HashMap<String, Object> map = new HashMap<>();

        if (loginVo.getUsername() == null || loginVo.getPassword() == null) {
            throw new Exception("账号或密码不能为空");
        }
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginVo.getUsername());
        UserDO userDO = userMapper.selectOne(queryWrapper);
    /*    String password = userDO.getPassword();
        String password1 = loginVo.getPassword();
        boolean b = md5Utils.validPassword(password1, password);*/
        String generate = PasswordUtils.generate("123");
        log.info("{}",generate);
        String password = userDO.getPassword();
        String password1 = loginVo.getPassword();
        boolean b = PasswordUtils.verify(password1,password );
        if (b) {
            HashMap<String, String> pyload = new HashMap<>();
            pyload.put("username", userDO.getUsername());
            pyload.put("utype", userDO.getUtype().toString());
            pyload.put("uauthority", userDO.getUauthority().toString());

            String token = jwtConfig.getToken(pyload);
            map.put("state", true);
            map.put("msg", "认证成功");
            map.put("token", token);//响应token
            return map;
        } else {
            throw new Exception("密码错误");
        }

    }

    @Override
    public List<UserDO> queryUser(UserVO userVo) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        if (userVo.getUsername() != null && !(userVo.getUsername().isEmpty())) {
            wrapper.eq(UserDO::getUsername, userVo.getUsername());
        }
        if (userVo.getUtype() != null) {
            wrapper.eq(UserDO::getUtype, userVo.getUtype());
        }
        if (userVo.getUauthority() != null) {
            wrapper.eq(UserDO::getUauthority, userVo.getUauthority());
        }
        List<UserDO> userDOS = userMapper.selectList(wrapper);
        return userDOS;
    }

    @Override
    public int deleteById(Integer id) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getUid, id);
        int i = userMapper.delete(wrapper);
        return i;
    }

    @Override
    public UserRespVO exitPerson(UserVO userVO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userVO, userDO);
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getUid,userVO.getUid());
        int update = userMapper.update(userDO, wrapper);
        return UserRespVO.builder().row(update).code(200).build();
    }
}
